package com.aiti.lulian.pay;


import com.aiti.lulian.pay.strategy.PayStrategy;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class PayFactory {

    @Resource
    @Qualifier("aliPayStrategy")
    private PayStrategy aliPayStrategy;

    @Resource
    @Qualifier("weChatPayStrategy")
    private PayStrategy wechatPayStrategy;

    public String getToken(String payType,String userId) {
        PayStrategyContext payStrategy = getPayStrategy(payType);
        return payStrategy.genToken(userId);
    }

    /**
     * 查询订单状态
     * @param orderId 订单号
     * @param payType 支付方式
     * @return
     */
    public String queryOrderStatus(String orderId,String payType) throws Exception {
        PayStrategyContext payStrategy = getPayStrategy(payType);
        return payStrategy.queryOrderStatus(orderId);
    }

    /**
     * 获取支付模式
     * @param type 支付类型  1:支付宝、2:微信
     * @return 具体的支付策略
     */
    public PayStrategyContext getPayStrategy(String type){
        switch (type){
            case "1":
                return new PayStrategyContext(aliPayStrategy);
            case "2":
                return new PayStrategyContext(wechatPayStrategy);
            default:
                throw new RuntimeException("获取支付策略失败");
        }
    }
}
